import cv2
import time
import camera_ctrl as ccl
import toolpack as tp
class cam_set_class:
    def __init__(self):
        print("use camera contral...")
    def info(self):
        print("hello")
    def show(self,str):
        print("input is ",str)
    a=0
    brightness=0
    contrast = 0
    saturation=0
    hue =0
    gain = 0
    expore = 0


def nothing(x):
    pass


#鼠标回调函数 
#事件 坐标 标志 返回

def show_num(event,y,x,flags,param):
    if event==cv2.EVENT_LBUTTONUP:
        #print('POS:(',x,y,') R:',r,'G:',g,'B:',b)
        #print('POS:(',x,y,')' ,img.item(x,y,0),img.item(x,y,1),img.item(x,y,2))
        print('POS:(',x,y,')' ,roi_img[x][y])
        #cv2.circle(roi_img,(y,x),10,(0,255,0),1)
        #cv2.putText(roi_img,str(roi_img[x][y]),(y,x), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),1)
    elif event==cv2.EVENT_MBUTTONDOWN:
        print(num)

cv2.namedWindow('image',cv2.WINDOW_NORMAL)
#cv2.namedWindow('zoomImage',1)
#cv2.resizeWindow('frame',800,600)

cv2.createTrackbar('Brightness','image',0,64,nothing)
cv2.createTrackbar('contrast','image',0,95,nothing)
cv2.createTrackbar('saturation','image',0,100,nothing)
cv2.createTrackbar('hue','image',0,4096,nothing)
cv2.createTrackbar('gain','image',0,60,nothing)
cv2.createTrackbar('expore','image',0,4096,nothing)


#cv2.setMouseCallback('zoomImage',show_num)

camset=cam_set_class()
camset.info()
#camset.a=100
#camset.show(camset.a)


#-------------------------------------
cap = cv2.VideoCapture(0)

img_width =1366
img_height=768

roi_width =840
roi_height=680

cap.set(cv2.CAP_PROP_FRAME_WIDTH, img_width)#//宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, img_height)#//高度
print(ccl.get_camera_info(cap))
#{'FRAME_WIDTH': 1280.0, 'FRAME_HEIGHT': 720.0, 'FPS': 10.0, 'BRIGHTNESS': 51.0, 'CONTRAST': 53.0,
#'SATURATION': 81.0, 'HUE': 0.0, 'GAIN': 38.0, 'EXPOSURE': 2496.0, 'FRAME_COUNT': -1.0}
cv2.setTrackbarPos('Brightness','image',51)
cv2.setTrackbarPos('contrast','image',53)
cv2.setTrackbarPos('saturation','image',81) 
cv2.setTrackbarPos('hue','image',2048)
cv2.setTrackbarPos('gain','image',33)
cv2.setTrackbarPos('expore','image',2496)



def send_parm(event,y,x,flags,param):
    if event==cv2.EVENT_LBUTTONUP:
        print('='*20)
        print('brightness',camset.brightness)
        print('contrast',camset.contrast)
        print('saturation',camset.saturation)
        print('hue',camset.hue)
        print('gain',camset.gain)
        print('expore',camset.expore)
        print('='*20)
        
        #cap.set(cv2.CAP_PROP_FPS, 30)#//帧率 帧/秒
        cap.set(cv2.CAP_PROP_BRIGHTNESS, camset.brightness)#//亮度 
        cap.set(cv2.CAP_PROP_CONTRAST,camset.contrast)#//对比度 40
        cap.set(cv2.CAP_PROP_SATURATION, camset.saturation)#//饱和度 50
        cap.set(cv2.CAP_PROP_HUE, camset.hue)#//色调 50
        cap.set(cv2.CAP_PROP_GAIN, camset.gain)#增益
        cap.set(cv2.CAP_PROP_EXPOSURE, camset.expore)#//曝光 50 
        print(ccl.get_camera_info(cap))

cv2.setMouseCallback('image',send_parm)
    
def get_ROI(img,pos,harf_d):
    start_point=[0,0]
    end_point = [0,0]
    start_point[0]=pos[0]-harf_d[0]
    start_point[1]=pos[1]-harf_d[1]

    end_point[0]=pos[0]+harf_d[0]
    end_point[1]=pos[1]+harf_d[1]
    
    #cv2.rectangle(frame,(start_point[0],start_point[1]),(end_point[0],end_point[1]),(255,0,255),2)
    #cv2.circle(frame,(int(img_width/2),int(img_height/2)),40,(255,0,255),2)
   
    return img[start_point[1]:end_point[1],start_point[0]:end_point[0]]

while(1):
    camset.brightness=cv2.getTrackbarPos('Brightness','image')
    camset.contrast  = cv2.getTrackbarPos('contrast','image')
    camset.saturation= cv2.getTrackbarPos('saturation','image')
    camset.hue       = cv2.getTrackbarPos('hue','image')-2048
    camset.gain      = cv2.getTrackbarPos('gain','image')
    camset.expore    = cv2.getTrackbarPos('expore','image')

    ret,frame = cap.read()
    #gray = cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
    #cv2.resizeWindow('frame',1027,768)
    
    
    
    #res=frame[100:400,300:400]
    
    
    #cv2.rectangle(frame,(872,1196),(1072,1396),(255,0,255),20)
    roi_img=get_ROI(frame,[int(img_width/2),int(img_height/2)],[int(roi_width/2),int(roi_height/2)])# W  H
    #cv2.rectangle()
    #res=tp.zoom(frame,0.6)
    #cv2.imshow('frame',tp.zoom(frame,0.6))
    cv2.imshow('zoomImage',roi_img)

    key=cv2.waitKey(1)&0xFF
    if key==27:
        print("exit ..")
        break
    elif key==ord('w'):
        print('='*20)
        print('brightness',camset.brightness)
        print('contrast',camset.contrast)
        print('saturation',camset.saturation)
        print('hue',camset.hue)
        print('gain',camset.gain)
        print('expore',camset.expore)
        print('='*20)
        
        #cap.set(cv2.CAP_PROP_FPS, 30)#//帧率 帧/秒
        cap.set(cv2.CAP_PROP_BRIGHTNESS, camset.brightness)#//亮度 
        cap.set(cv2.CAP_PROP_CONTRAST,camset.contrast)#//对比度 40
        cap.set(cv2.CAP_PROP_SATURATION, camset.saturation)#//饱和度 50
        cap.set(cv2.CAP_PROP_HUE, camset.hue)#//色调 50
        cap.set(cv2.CAP_PROP_GAIN, camset.gain)#增益
        cap.set(cv2.CAP_PROP_EXPOSURE, camset.expore)#//曝光 50 
        print(ccl.get_camera_info(cap))

    elif key==ord('r'):
        print(ccl.get_camera_info(cap))
    elif key == ord('s') :# s press
        name_by_date = time.strftime("%Y%m%d_%H%M%S", time.localtime())
        #name_by_date=''
        cv2.imwrite('./img/cam/'+name_by_date+'_g_'+str(camset.gain)+'_e_'+str(camset.expore)+'.png',frame)
        b,g,r=cv2.split(roi_img)
        gray_tif=cv2.merge([r,r,r])
        cv2.imwrite('./img/cam/'+name_by_date+'_g_'+str(camset.gain)+'_e_'+str(camset.expore)+'.tif',gray_tif)
        print("save..")

    if camset.brightness==-1:
        print("exit 0 ..")
        break
cap.release()
cv2.destroyAllWindows()